/**
 * Created by WYH on 17/4/19.
 */
angular.module('tribalc').controller(
    'RentController', [
        '$rootScope' ,'$scope', 'Api', 'RentProtocolModel', 'RentBuckleModel', 'RentPaymentModel','RentPlanItemModel', '$timeout','$state','Alert',
        function ($rootScope ,$scope, Api, RentProtocolModel, RentBuckleModel, RentPaymentModel, RentPlanItemModel, $timeout,$state,Alert) {


        $scope.showAllPlanItem = false;
        $scope.showSubmitRent = false;
        $scope.showSubmitBuckle = false;
        $scope.showChangeSubmitRent=false;
        $scope.isProperty = false;

        $scope.submitBuckle = submitBuckle;
        $scope.changeVerifyCodeBtn = changeVerifyCodeBtn;
        $scope.sendVerifyCode = sendVerifyCode;
        $scope.submitRentProtocol = submitRentProtocol;
        $scope.ChangeSubmitRent=ChangeSubmitRent;
        $scope.checkCountBankMoney=checkCountBankMoney;

        // var companyID = $state.companyId;
        // console.log(companyID);
        getRentDetail();
        // getBuckleDetail();
        getRentPayment();
        checkProperty();

        $rootScope.$watch('RootCompanyInfo',function () {
            checkProperty()
        });
        function checkProperty() {
            if($rootScope.RootCompanyInfo && $rootScope.RootCompanyInfo.propertyNature==true){
                $state.go('company.companyProfile')
            }
        }
        function ChangeSubmitRent(number,money) {
            if(!number||!money){
                return Alert.alertBox('请检查输入信息')
            }
            Api.changeSubmitPay($scope.rentPayment.id, money, number, function (result) {
                Alert.alertBox('修改成功',function () {
                   return window.location.reload()
                });
            })
        }
        function submitRentProtocol(number, money) {
            if(!number||!money){
                return Alert.alertBox('请检查输入信息')
            }
            Api.tenantsSubmitPay(null, money, number, function (result) {
                Alert.alertBox('提交成功',function () {
                   return window.location.reload()
                });
            })
        }
         function checkBankNumber(value){
            return /^\d{16}|\d{19}$/.test(value);
        }


        function sendVerifyCode(phone) {
              if (phone) {
                  Api.sendPhoneVerifyCode(phone, function (result) {
                      changeVerifyCodeBtn();
                  })
              } else {
                  Alert.alertBox('请输入原手机号');
              }
          }


        function changeVerifyCodeBtn() {

            var wait = 60;
            forbidSendVerification();

            function forbidSendVerification() {
                if (wait == 0) {
                    $scope.verifyCodeBtn = '获取验证码';
                    wait = 60;
                } else {
                    $scope.verifyCodeBtn = "重新发送(" + wait + ")";
                    wait--;
                    $timeout(function () {
                        forbidSendVerification()
                    }, 1000)
                }
            }
        }
        function checkCountBankMoney(number) {
            var pattern =/^[0-9]+([.]\d{1,2})?$/;
            if(number && !pattern.test(number)){
                Alert.alertBox('最多保留两位小数');
                $scope.editBankMoney=Number(number.toFixed(2));
            }
            if(number<0){
                Alert.alertBox('输入值不可为负数')
                $scope.editBankMoney=0;
            }
        }

        function submitBuckle(buckle) {
            var buckleModel = new RentBuckleModel(buckle);
            console.log(buckleModel);
            if(buckleModel.userName && buckleModel.bankCardNum && buckleModel.bankName && buckleModel.phone && buckleModel.bankCode && buckleModel.idNo){
               Api.createBuckle(buckleModel,$scope.rentProtocol.id, function (result) {
                Alert.alertBox('创建代扣信息成功',function () {
                    return window.location.reload();
                });

            });

            }else {
                Alert.alertBox('请检查输入信息是否完整')
            }

        }

        function getRentDetail() {

            Api.getRentDeail(null, function (result) {
                if (result.length) {
                    Alert.alertBox('该公司无租赁协议');
                    $rootScope.id = result.data[0].id
                } else {
                    $scope.rentProtocol = initialRentProtocol(result.data);
                    getPlanItemList($scope.rentProtocol.id);
                    getBuckleDetail($scope.rentProtocol.id);
                }
            });
        }

        function initialRentProtocol(protocolList) {
            var rentProtocol;
            for (var i = 0; i < protocolList.length; i++) {
                $rootScope.id = protocolList[i].id
                var protocol = protocolList[i];
                if (protocol.status == RentProtocolModel.ProtocolStatus.actived) {
                    rentProtocol = new RentProtocolModel(protocol);
                    break;
                }
            }
            if (!rentProtocol) {
                rentProtocol = new RentProtocolModel();
            }
            return rentProtocol;
        }


        function getBuckleDetail(rentProtocolID) {
            Api.getRentBuckle(rentProtocolID, function (result) {
                // $scope.showSubmitBuckle = true;
                $scope.rentBuckle = new RentBuckleModel(result.data);
            })
        }


        function getRentPayment() {
            Api.getRentPayment(null, function (result) {
              if (result.data.length) {
                  $scope.rentPayment = new RentPaymentModel(result.data[0]);
              }
            })
        }

        function getPlanItemList(rentId) {
            Api.getRentPlanitemList(null, rentId, function (result) {
                $scope.rentPlanItemList = RentPlanItemModel.setupPlanItemList(result.data);

            })
        }




    }]
);